﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Text;

public partial class MRP_Purchase : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void UIServer1_DBBeforeUpdate(object sender, Evolutility.UIServer.MRPDataUpdateEventArgs e)
    {

    }
    protected void UIServer1_CmdFieldFire(object sender, Evolutility.UIServer.CmdFieldEventArgs e)
    {
        int id = Int32.Parse(e.PK);
        StringBuilder sbSQL = new StringBuilder();

        int materialID=0;
        int quantity=0;
        float ConversionRatio = 1;

        if (e.CommandText.Equals("发布"))
        {
            Database db = DatabaseFactory.CreateDatabase("MRPSqlServer");

            string ssSQL = string.Format("Select MaterialID,Quantity,ConversionRatio from MRP_PurchaseDetail where orderid={0}", id);
            //取得MaterialID
            using (IDataReader reader = db.ExecuteReader(CommandType.Text, ssSQL))
            {
                while(reader.Read())
                {
                    materialID=int.Parse(reader[0].ToString());
                    quantity=int.Parse(reader[1].ToString());
                    ConversionRatio = float.Parse(reader[2].ToString());
                    //if (materialID > 0)
                    {

                        sbSQL.AppendFormat("Update dbo.MRP_Stock set InTransit=InTransit+{1} where MRP_Stock.MaterialID = {0}", materialID, quantity * ConversionRatio);
                        //db.ExecuteNonQuery(CommandType.Text, sSQL);
                    }
                }
                if (materialID > 0)
                {
                    sbSQL.AppendFormat("Update dbo.MRP_Purchase set Status=2,CommitDate=getdate() where ID = {0}", id);
                    sbSQL.AppendFormat("Update dbo.MRP_PurchaseDetail set Status=2 where OrderID = {0}", id);
                    db.ExecuteNonQuery(CommandType.Text, sbSQL.ToString());
                }

            }

            
        }
        else if (e.CommandText.Equals("取消订单"))
        {
            Database db = DatabaseFactory.CreateDatabase("MRPSqlServer");
            string ssSQL = string.Format("Select MaterialID,Quantity,ConversionRatio from MRP_PurchaseDetail where orderid={0}", id);
            //取得MaterialID
            using (IDataReader reader = db.ExecuteReader(CommandType.Text, ssSQL))
            {
                while (reader.Read())
                {
                    materialID = int.Parse(reader[0].ToString());
                    quantity = int.Parse(reader[1].ToString());
                    ConversionRatio = float.Parse(reader[2].ToString());
                    //if (materialID > 0)
                    {

                        sbSQL.AppendFormat("Update dbo.MRP_Stock set InTransit=InTransit-{1} where MRP_Stock.MaterialID = {0}", materialID, quantity * ConversionRatio);
                        //db.ExecuteNonQuery(CommandType.Text, sSQL);
                    }
                }
            }
            if (materialID > 0)
            {
                sbSQL.AppendFormat("Update dbo.MRP_PurchaseDetail set Status=4,notes=notes+'被用户取消。' where OrderID = {0};", id);
                sbSQL.AppendFormat("Update dbo.MRP_Purchase set Status=4,notes=notes+'被用户取消。' where ID = {0}", id);
                db.ExecuteNonQuery(CommandType.Text, sbSQL.ToString());
            }


        }
        else if (e.CommandText.Equals("手工关闭"))
        {
            Database db = DatabaseFactory.CreateDatabase("MRPSqlServer");
            string ssSQL = string.Format("Select MaterialID,[RemainImportQuantity],[ConversionRatio] from [View_GetRemainImportQuantity] where orderid={0}", id);
            //取得MaterialID
            using (IDataReader reader = db.ExecuteReader(CommandType.Text, ssSQL))
            {
                while (reader.Read())
                {

                    materialID = int.Parse(reader["MaterialID"].ToString());

                    quantity = int.Parse(reader["RemainImportQuantity"].ToString());
                    ConversionRatio = float.Parse(reader["ConversionRatio"].ToString());
                    //if (materialID > 0)
                    {

                        sbSQL.AppendFormat("Update dbo.MRP_Stock set InTransit=InTransit-{1} where MRP_Stock.MaterialID = {0}", materialID, quantity * ConversionRatio);
                        //db.ExecuteNonQuery(CommandType.Text, sSQL);
                    }
                }
            }
            if (materialID > 0)
            {
                sbSQL.AppendFormat("Update dbo.MRP_PurchaseDetail set Status=5,notes=notes+'被用户关闭。' where OrderID = {0};", id);
                sbSQL.AppendFormat("Update dbo.MRP_Purchase set Status=5,notes=notes+'被用户关闭。' where ID = {0}", id);
                db.ExecuteNonQuery(CommandType.Text, sbSQL.ToString());
            }


        }
    }
}
